home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Zoners Half-Life Tools / hlvis / zones.cpp < prev    next >
C/C++ Source or Header  |  2000-09-12  |  4KB  |  156 lines

  1. // Copyright (C) 2000  Sean Cavanaugh
  2. // This file is licensed under the terms of the Lesser GNU Public License
  3. // (see LPGL.txt, or http://www.gnu.org/copyleft/lesser.txt)
  4.  
  5. #include "vis.h"
  6.  
  7.  
  8. void Zones::set(UINT32 zone, const BoundingBox& bounds)
  9. {
  10.     if (zone < m_ZoneCount)
  11.     {
  12.         m_ZoneBounds[zone] = bounds;
  13.     }
  14. }
  15.  
  16. UINT32 Zones::getZoneFromBounds(const BoundingBox& bounds)
  17. {
  18.     UINT32 x;
  19.     for (x=0; x<m_ZoneCount; x++)
  20.     {
  21.         if (m_ZoneBounds[x].testSuperset(bounds))
  22.         {
  23.             return x;
  24.         }
  25.     }
  26.     return 0;
  27. }
  28.  
  29. UINT32 Zones::getZoneFromWinding(const Winding& winding)
  30. {
  31.     UINT32          x;
  32.     BoundingBox     bounds;
  33.  
  34.     for (x=0; x<winding.m_NumPoints; x++)
  35.     {
  36.         bounds.add(winding.m_Points[x]);
  37.     }
  38.  
  39.     return getZoneFromBounds(bounds);
  40. }
  41.  
  42. // BORROWED FROM HLRAD
  43. // TODO: Consolite into common sometime
  44. static Winding*      WindingFromFace(const dface_t* f)
  45. {
  46.     int             i;
  47.     int             se;
  48.     dvertex_t*      dv;
  49.     int             v;
  50.     Winding*        w = new Winding(f->numedges);
  51.  
  52.     for (i = 0; i < f->numedges; i++)
  53.     {
  54.         se = g_dsurfedges[f->firstedge + i];
  55.         if (se < 0)
  56.         {
  57.             v = g_dedges[-se].v[1];
  58.         }
  59.         else
  60.         {
  61.             v = g_dedges[se].v[0];
  62.         }
  63.  
  64.         dv = &g_dvertexes[v];
  65.         VectorCopy(dv->point, w->m_Points[i]);
  66.     }
  67.  
  68.     return w;
  69. }
  70.  
  71. Zones* MakeZones(void)
  72. {
  73.     UINT32 x;
  74.     UINT32 func_vis_count = 0;
  75.  
  76.     ParseEntities();
  77.  
  78.     // Note: we arent looping through entities because we only care if it has a winding/bounding box
  79.  
  80.     // First count the number of func_vis's
  81.     for (x=0; x<g_nummodels; x++)
  82.     {
  83.         entity_t*       ent = EntityForModel(x);
  84.  
  85.         if (!strcasecmp(ValueForKey(ent, "classname"), "func_vis"))
  86.         {
  87.             UINT32 value = atoi(ValueForKey(ent, "node"));
  88.             if (value)
  89.             {
  90.                 func_vis_count++;
  91.             }
  92.             else
  93.             {
  94.                 Error("func_vis with no \"node\" id\n");
  95.             }
  96.         }
  97.     }
  98.  
  99.     if (!func_vis_count)
  100.     {
  101.         return NULL;
  102.     }
  103.  
  104.     Zones* zones = new Zones(func_vis_count);
  105.  
  106.     for (x=0; x<g_nummodels; x++)
  107.     {
  108.         dmodel_t*       mod = g_dmodels + x;
  109.         entity_t*       ent = EntityForModel(x);
  110.  
  111.         if (!strcasecmp(ValueForKey(ent, "classname"), "func_vis"))
  112.         {
  113.             UINT32 func_vis_id = atoi(ValueForKey(ent, "node"));
  114.  
  115.             {
  116.                 epair_t* keyvalue;
  117.     
  118.                 for (keyvalue = ent->epairs; keyvalue; keyvalue = keyvalue->next)
  119.                 {
  120.                     UINT32 other_id = atoi(keyvalue->key);
  121.                     if (other_id)
  122.                     {
  123.                         zones->flag(func_vis_id, other_id);
  124.                     }
  125.                 }
  126.             }
  127.     
  128.             {
  129.                 UINT32          j;
  130.                 BoundingBox     bounds;
  131.                 dface_t*        f = g_dfaces + mod->firstface;
  132.             
  133.                 for (j = 0; j < mod->numfaces; j++, f++)
  134.                 {
  135.                     Winding*        w = WindingFromFace(f);
  136.                     UINT32          k;
  137.  
  138.                     for (k = 0; k < w->m_NumPoints; k++)
  139.                     {
  140.                         bounds.add(w->m_Points[k]);
  141.                     }
  142.                     delete w;
  143.                 }
  144.  
  145.                 zones->set(func_vis_id, bounds);
  146.  
  147.                 Log("Adding zone %u : mins(%4.3f %4.3f %4.3f) maxs(%4.3f %4.3f %4.3f)\n", func_vis_id, 
  148.                     bounds.m_Mins[0],bounds.m_Mins[1],bounds.m_Mins[2],
  149.                     bounds.m_Maxs[0],bounds.m_Maxs[1],bounds.m_Maxs[2]);
  150.             }
  151.         }
  152.     }
  153.  
  154.     return zones;
  155. }
  156.